<template>
  <div class="address">
    <MyHeader titles="收货地址" lefts="返回" :upPage="false" url="/user/address" />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
    <van-empty description="暂无地址" v-if="noAddres" />
  </div>
</template>

<script>
import { AddressList } from "@/request/api";
import MyHeader from "@/components/Home/Header";

export default {
  created() {
    AddressList().then(res => {
      if (res.errno == 0) {
        if(res.data.length > 1){
          this.noAddres = false
        }
        var arr = JSON.parse(
          JSON.stringify(res.data)
            .replace(/mobile/g, "tel")
            .replace(/is_default/g, "isDefault")
        );
        let first = [];
        arr.map((v, k) => {
          v.address =
            v.province_name + v.city_name + v.district_name + v.address;
          // 如果是默认地址，放到第一位
          if (v.isDefault == 1) {
            this.chosenAddressId = v.id;
            first = v;
            arr.splice(k, 1);
          }
        });
        arr.unshift(first);
        this.list = arr;
      }
    });
  },
  components: {
    MyHeader
  },
  data() {
    return {
      chosenAddressId: 0,
      list: [],
      noAddres: true
    };
  },
  methods: {

    onAdd() {
      this.$router.push("/user/editaddress/add");
    },
    onEdit(item) {
      this.$router.push("/user/editaddress/" + item.id);
    }
  }
};
</script>

<style lang="less" scoped>
.address {
  /deep/.van-address-list__bottom {
    bottom: 55px;
  }
}
</style>